{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 资产定价入门\n",
    "### 1. 利率\n",
    "利率是决定金融产品价格的一个基本因素。\n",
    "\n",
    "利率定义了资金借入方承若支付给资金借出方的资金数量\n",
    "\n",
    "LIBOR是伦敦同业银行的拆借利率\n",
    "\n",
    "SHIBOR是我国仿照LIBOR的模式，建立的上海同业拆借利率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 利率的计量\n",
    "谈论年利率的时候，根据复利频率的不同，会有不同的表现形式。\n",
    "\n",
    "假设年利率是10%，那么可以是一年按10%复利一次，也可以是半年按5%复利两次，还可以是每3个月按2.5%复利4次 \n",
    "\n",
    "计算公式：$A(1 + R/m)^{mn}$， 其中R是利率，m是一年的复利次数，n表达求n年的投资终值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对复利m次的投资终值进行一个模拟操作。\n",
    "假设初始投资为100元，年利率为10%，投资1年，复利次数从1到15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x2be6f5881d0>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAmMAAAF3CAYAAADpZ0xtAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzt3Xt8nGWd9/HvL+fzoWlamqQ0PUBLaUsLAVqqLupqEeQsWmRZWGRZd0FcDyDorqyPICgKLvKwyoNQXNlSQOiygBREtK4k1NLzgUKTnpKe0qQ5nzPX88dM0jSdNKdJ7pnM5/165TUz99xz5zd32+Tb6/7NdZlz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xl1++eX9vrcvfOELuuGGG3TxxRdLkm688UZdeumlOuecc3T99dersbFRkvToo4/qggsuOO61y5Yt09q1a/Xoo49Kkj772c/qm9/8pi688EK98cYbuueee9Ta2qrp06frqaeeCukIHQAA4WCkBmRCIaouU/YOYv1tH4if//znysvL09tvv63s7Ozjnjtw4ID+93//V6+88oruuusuSf45u1566SWtW7dOb7/9tr7xjW9oIHO9LV26VCtWrPDX29amt956SxdffLEmTJigN998U+vWrdOKFSt0++23D7j2I0eO6N5779Xvfvc7rVu3TkVFRXrooYcG8e4BADjeSLQDDZXP59TQ2qHDdS36wWvbu4NYl+b2Tj24aodH1R0zpkbGTjaCJUmLH/i9KmqaT9ien5WsFf+wKOT1XHHFFYqJidHs2bN16NAhSf55u7797W9r9erViomJUUVFhQ4dOqRTTjnlpMf6zGc+o9tvv12tra16/fXX9bGPfUzJycmqra3Vbbfdpg0bNig2NlYffPDBgOsrKSnRtm3btHjxYkn+kLdoUejPAwAgOgxn9Mnnc2pu71RjW4eaWjvV0NqhprZjjxtbO/z32wL3WzvU2NapprYONXY/f/zj3uErmP1BcsFoG1NhrD93LJl53F8SSUqOj9UdS2aOyPdLTEzsvt81+vXMM8+osrJS7733nuLj41VYWDigObySkpJ04YUXatWqVVqxYoWuvfZaSdLDDz+siRMnauPGjfL5fEpKSjrhtXFxcfL5jo3+dX0/55w+9alPafny5cN6nwCA6OOcU2uHT3Ut7Wpo6VBDa4fufXVb0NGn76zcrD/vPNIdrvxhKhCcAuGqqa3/4NQlLsaUmhin1IRYpSTGdd/PSklQamLssecS4pSa6L/9yRs7dLSp/YRj5WUlD/tcDFdUhbGuVO5l815tba0mTJig+Ph4vf3229qzZ8+AX7t06VI98cQTWrt2rZYtW9Z9vIKCAsXExOjpp59WZ+eJf5kLCwv12GOPyefzqaKiQmvW+NduX7hwoW699Vbt3LlTM2bMUFNTk8rLy3X66aeH5L0CAEbGcBrRu0JUfSBA1QfCVH1rh/+2pd2/vfuxf7+ufbqeb2jpUIdvY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ijAEAAHiIMAYAAOCh/w8bxoW17J5OFQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "data = pd.DataFrame()\n",
    "data['frequency'] = np.arange(1, 15, 1)\n",
    "data['final_value'] = 100 * ((1 + 0.1 / data['frequency']) ** data['frequency'])\n",
    "data.plot(figsize = (10, 6), x = 'frequency', y = 'final_value', style = 'o-')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到，随着复利频率的增加，投资终值趋近于一个极限。\n",
    "可以从数学上证明，当复利频率趋于无穷大的时候，投资的终值为：$Ae^{Rn}$\n",
    "这种情况下的利率称为连续复利(continuous compounding)利率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 零息利率\n",
    "\n",
    "N年的零息利率（zero rate）是指今天投入的资金在N年后所得的收益率。\n",
    "\n",
    "所有的利息以及本金在N年末支付给投资者，在N年满期以前，投资不支付任何利息收益。\n",
    "\n",
    "零息利率也称为即期利率（spot rate）\n",
    "\n",
    "在市场上直接观察到的很多利率都不是零息利率。因为债券的一些券息并不是在到期的时候支付，而是在到期前就会周期性地支付一部分。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. 债券定价\n",
    "任何金融产品的理论价格都等于未来现金流的折现值。\n",
    "\n",
    "有时，投资者使用同样的贴现率对债券的现金流进行铁线，但更精确的方法是对不同的现金流采用不同的零息贴现率。\n",
    "\n",
    "#### 4.1 债券收益率\n",
    "债券收益率又称为到期收益率（Yield To Maturity， YTM）。\n",
    "\n",
    "债券收益率等于对所有现金流贴现并使债券的价格于市场价格相等的贴现率。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.06759816]\n"
     ]
    }
   ],
   "source": [
    "from scipy.optimize import fsolve\n",
    "import math\n",
    "\n",
    "def func(y):\n",
    "    e = math.e\n",
    "    return 3*e**(-y*0.5) + 3 * e**(-y*1) + 3 * e**(-y*1.5) + 103 * e**(-y*2) - 98.39\n",
    "\n",
    "y = fsolve(func, 0.1)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.2 平价收益率\n",
    "债券平价收益率（par yield）是使债券价格等于面值（par value）的全息率。\n",
    "\n",
    "假定债券每年的券息为c，每半年支付一次，即c/2，采用之前的零息利率，得到如下方程：\n",
    "\n",
    "$c/2*e^{-0.05*0.5} + c/2*e^{-0.058*1} + c/2*e^{-0.064*1.5} + (100 + c/2)*e^{-0.068*2} = 100$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6.87287617]\n"
     ]
    }
   ],
   "source": [
    "from scipy.optimize import fsolve\n",
    "import math\n",
    "\n",
    "def func(c):\n",
    "    e = math.e\n",
    "    return c/2*e**(-0.05*0.5) + c/2*e**(-0.058*1) + c/2*e**(-0.064*1.5) + (100+c/2)*e**(-0.068*2) - 100\n",
    "\n",
    "c = fsolve(func, 0.1)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.3 国债零息利率确定\n",
    "确定零息收益率曲线的一种常用方法是从国债价格入手来进行计算。\n",
    "\n",
    "最流行的方法是所谓的票息剥离法（bootstrap method）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>maturity</th>\n",
       "      <th>coupon</th>\n",
       "      <th>principal</th>\n",
       "      <th>price</th>\n",
       "      <th>zero_rate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.25</td>\n",
       "      <td>0</td>\n",
       "      <td>100</td>\n",
       "      <td>97.5</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.50</td>\n",
       "      <td>0</td>\n",
       "      <td>100</td>\n",
       "      <td>94.9</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>100</td>\n",
       "      <td>90.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.50</td>\n",
       "      <td>8</td>\n",
       "      <td>100</td>\n",
       "      <td>96.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.00</td>\n",
       "      <td>12</td>\n",
       "      <td>100</td>\n",
       "      <td>101.6</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   maturity  coupon  principal  price  zero_rate\n",
       "0      0.25       0        100   97.5        NaN\n",
       "1      0.50       0        100   94.9        NaN\n",
       "2      1.00       0        100   90.0        NaN\n",
       "3      1.50       8        100   96.0        NaN\n",
       "4      2.00      12        100  101.6        NaN"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "data = pd.DataFrame()\n",
    "data['maturity'] = [0.25, 0.5, 1, 1.5, 2]\n",
    "data['coupon'] = [0, 0, 0, 8, 12]\n",
    "data['principal'] = [100] * 5\n",
    "data['price'] = [97.5, 94.9, 90, 96, 101.6]\n",
    "data['zero_rate'] = [np.nan] * 5\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面计算得到0.25年，0.5年，1年期的零息利率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.101271\n",
      "1    0.104693\n",
      "2    0.105361\n",
      "Name: zero_rate, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "from sympy.solvers import solve\n",
    "from sympy import Symbol\n",
    "import math\n",
    "\n",
    "data.loc[0:2, 'zero_rate'] = data.loc[0:2, 'principal'] / data.loc[0:2, 'price']\n",
    "data.loc[0:2, 'zero_rate'] = data['zero_rate'].apply(math.log)\n",
    "data.loc[0:2, 'zero_rate'] = data.loc[0:2, 'zero_rate'] / data.loc[0:2, 'maturity']\n",
    "print(data.loc[0:2, 'zero_rate'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 9.10 影响期权价格的因素\n",
    "有六种因素会影响股票期权的价格，如下：\n",
    "- 当前股票价格 $S_0$\n",
    "- 执行价格 K\n",
    "- 期权期限 T\n",
    "- 股票价格的波动率 σ\n",
    "- 无风险利率 r\n",
    "- 期权期限内预期发放的股息\n",
    "\n",
    "为期权定价，最常用的公式是**布莱克-斯科尔斯-默顿（Black-Scholes-Metron）定价公式**：\n",
    "\n",
    "$call price 看涨期权价格 c = S_0N(d_1) - Ke^{-rT}N(d_2)$\n",
    "\n",
    "$put price 看跌期权价格 p = Ke^{-rT}N(-d_2) - S_0N(-d_1)$\n",
    "\n",
    "其中，\n",
    "\n",
    "$d_1 = \\frac{ln(S_0/K) + (r + \\sigma^2/2)T}{\\sigma\\sqrt{T}}$\n",
    "\n",
    "$d_2 = \\frac{ln(S_0/K) + (r - \\sigma^2/2)T}{\\sigma\\sqrt{T}} = d_1 - \\sigma\\sqrt{T}$\n",
    "\n",
    "其中，函数N(x)为标准正太分布的累积概率分布函数（计算call_price和put_price中的N(x)）\n",
    "\n",
    "c与p分别为欧式看涨期权与看跌期权的价格\n",
    "\n",
    "$S_0$为股票为0时刻的价格\n",
    "\n",
    "K为执行价格\n",
    "\n",
    "r为连续复利的无风险利率\n",
    "\n",
    "$\\sigma$为股票价格的波动率\n",
    "\n",
    "T为期权的期限"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用Python实现期权定价的函数\n",
    "option_pricer函数共有6个参数，每个参数在注释里都有说明"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "参数：\n",
    "    call_or_put: 1代表看涨期权价格，0代表看跌期权价格\n",
    "    spot：标的资产价格\n",
    "    strike：执行价格\n",
    "    maturity：到期时间（年化）\n",
    "    r：无风险利率\n",
    "    vol：波动率\n",
    "返回值：\n",
    "    相应期权的价格\n",
    "\"\"\"\n",
    "from math import log\n",
    "from math import sqrt\n",
    "from scipy.stats import norm\n",
    "from math import exp\n",
    "def option_pricer(call_or_put, spot, strike, maturity, r, vol):\n",
    "    d1 = (log(spot / strike) + (r + 0.5 * vol * vol) * maturity) / vol / sqrt(maturity)\n",
    "    d2 = d1 - vol * sqrt(maturity)\n",
    "    \n",
    "    if call_or_put == 1:\n",
    "        call_price = spot * norm.cdf(d1) - strike * exp(-r * maturity) * norm.cdf(d2)\n",
    "        return call_price\n",
    "    elif call_or_put ==0:\n",
    "        put_price = strike * exp(-r * maturity) * norm.cdf(-d2) - spot * norm.cdf(-d1)\n",
    "        return put_price"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "利用一个例子，来使用上面的函数计算\n",
    "\n",
    "考虑一个6个月的期权，股票的当前价格为50元，期权执行价格为40元。无风险利率为3%，年化波动率为20%。\n",
    "\n",
    "转换成函数的参数：\n",
    "\n",
    "$S_0 = 50$\n",
    "\n",
    "$K = 40$\n",
    "\n",
    "$r = 0.03$\n",
    "\n",
    "$\\sigma = 0.2$\n",
    "\n",
    "$T = 6 / 12 = 0.5$\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10.714669847585476"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 假设该期权为看涨期权，则\n",
    "option_pricer(1, 50, 40, 0.5, 0.03, 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1191474317079777"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 假设该期权为看跌期权，则\n",
    "option_pricer(0, 50, 40, 0.5, 0.03, 0.2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
